package com.example.lprxjava;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.AsyncSubject;
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.ReplaySubject;

/**
 * Author by lp,on 2021/8/17/017,14:27.
 * Rxjava subject
 */
public class SubjectFragement extends BaseFragment {

    private static final String TAG = "SubjectFragement";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: ");
    }

    @Override
    String getTAG() {
        return TAG;
    }

    @Override
    String getTitle() {
        return "RxJava subject";
    }

    @Override
    int getFragmentLayoutId() {
        return R.layout.subject_frqagment;
    }

    @Override
    void onLoad(View view) {
        //只接受到最后一条发送的数据。
        asyncSubject();
        //只接收到订阅前最后一条数据，以及订阅后的所有数据。
        beheviorSubject();
        //会接收到全部数据，无论订阅前后。
        replaySubject();
        //只会接收到订阅后的数据。
        publishSubject();


    }

    private void publishSubject() {
        //只会接收到订阅后的数据。
        PublishSubject<Object> subject = PublishSubject.create();
        subject.onNext("PublishSubject 1");
        subject.onNext("PublishSubject 2");
//        subject.onComplete();
        subject.onNext("PublishSubject 3");
//        subject.compose(new SchedulerTransformer<>());
        subject.subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                Log.i(TAG, "accept: " + o);
                textView.setText(textView.getText()+"\r\n"+o.toString());
            }
        });
        subject.onNext("PublishSubject 4");
        subject.onComplete();
    }

    private void replaySubject() {
        //会接收到全部数据，无论订阅前后。
        ReplaySubject<Object> subject = ReplaySubject.create();
        subject.onNext("ReplaySubject 1");
        subject.onNext("ReplaySubject 2");
        subject.onComplete();
        subject.onNext("ReplaySubject 3");
        subject.compose(new SchedulerTransformer<>());
        subject.subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                Log.i(TAG, "accept: " + o);
                textView.setText(textView.getText()+"\r\n"+o.toString());
            }
        });
        subject.onNext("ReplaySubject 4");
    }

    private void beheviorSubject() {
        //只接收到订阅前最后一条数据，以及订阅后的所有数据。
        BehaviorSubject<Object> subject = BehaviorSubject.create();
        subject.onNext("beheviorSubject 1");
        subject.onNext("beheviorSubject 2");
        subject.onNext("beheviorSubject 3");
//        subject.compose(new SchedulerTransformer<>());
        subject.subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                Log.i(TAG, "accept: " + o);
                textView.setText(textView.getText()+"\r\n"+o.toString());
            }
        });
        subject.onComplete();
        subject.onNext("beheviorSubject 4");
    }

    private void asyncSubject() {
        //只接受到最后一条发送的数据。
        AsyncSubject<Object> subject = AsyncSubject.create();
        subject.onNext("AsyncSubject 1");
        subject.onNext("AsyncSubject 2");
        subject.onComplete();
        subject.onNext("AsyncSubject 3");
        subject.compose(new SchedulerTransformer<>());
        subject.subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                Log.i(TAG, "accept: " + o);
                textView.setText(textView.getText()+"\r\n"+o.toString());
            }
        });
        subject.onNext("AsyncSubject 4");
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "onDestroyView: ");

    }
}
